<template>
    <div class="login-wrap">
        <div class="ms-login">
            <div class="ms-title">校友网站后台管理系统</div>
            <el-form :model="loginForm" :rules="rules" ref="login" label-width="0px" class="ms-content">
                <el-form-item prop="username">
                    <el-input v-model="loginForm.username" placeholder="请输入用户名">
                        <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="password">
                    <el-input
                        type="password"
                        placeholder="请输入密码"
                        v-model="loginForm.password"
                        @keyup.enter.native="submitForm()"
                    >
                        <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
                    </el-input>
                </el-form-item>
                <el-form-item prop="captcha">
                    <div class="captcha">
                        <el-input placeholder="请输入验证码" v-model="loginForm.captcha" >
                            <el-button slot="prepend" icon="el-icon-place"></el-button>
                        </el-input>
                        <img :src="captchaUrl" @click="updateCaptcha" style="height: 32px">
                    </div>
                </el-form-item>
                <div class="login-btn">
                    <el-button type="primary" @click="submitForm()">登录</el-button>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script>
import { postRequest } from '../../api';

export default {
    data() {
        return {
            loginForm: {
                username: '',
                password: '',
                captcha: ''
            },
            rules: {
                username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
                password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
                captcha: [{required: true, message: '请输入验证码', trigger: 'blur'}]
            },
            captchaUrl: 'api/captcha?time='+new Date()
        }
    },
    methods: {
        submitForm() {
            this.$refs.login.validate(valid => {
                if (valid) {
                    postRequest('/backStageLoginValidate',this.loginForm).then(resp=>{
                        if(resp){
                            if(resp.success){
                                sessionStorage.setItem('ms_username', this.loginForm.username);
                                sessionStorage.setItem('token', resp.token);
                                sessionStorage.setItem('userId', resp.entity.userId);
                                sessionStorage.setItem('userNick', resp.entity.nick);
                                sessionStorage.setItem('avatar', resp.entity.avatarUrl);
                                sessionStorage.setItem('currentUser', JSON.stringify(resp.entity));
                                this.$message.success('登录成功！');
                                this.$router.push('/');
                            }else{
                                this.$message.error(resp.message);
                                this.updateCaptcha();
                                return false;
                            }
                        }else{
                            this.$message.error('未知错误！');
                            this.updateCaptcha();
                            return false;
                        }
                    });
                } else {
                    this.$message.error('请输入账号、密码和验证码');
                    this.updateCaptcha();
                    return false;
                }
            });
        },
        updateCaptcha(){
            this.captchaUrl='api/captcha?time='+new Date();
        }
    },
    mounted() {

    }
};
</script>

<style scoped>
.login-wrap {
    position: relative;
    width: 100%;
    height: 100%;
    background-image: url(../../assets/img/yc-library2.jpg);
    background-size: 100%;
}
.ms-title {
    width: 100%;
    line-height: 50px;
    text-align: center;
    font-size: 20px;
    color: black;
    border-bottom: 1px solid #ddd;
}
.ms-login {
    position: absolute;
    left: 50%;
    top: 50%;
    width: 350px;
    margin: -190px 0 0 -175px;
    border-radius: 5px;
    background: rgba(255, 255, 255, 0.7);
    overflow: hidden;
}
.ms-content {
    padding: 30px 30px;
}
.el-input{
    height: 100%;
}
.captcha{
    display: flex;
    flex-direction: row;
}
.login-btn {
    text-align: center;
}
.login-btn button {
    width: 100%;
    height: 36px;
    margin-bottom: 10px;
}
</style>